package com.ywjt.platformbusiness.market.integralgoods.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.classify.model.FistLevelTypeModel;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.home.CommonModel;
import com.ywjt.platformbusiness.home.adapter.HomeGoodsTypeAdapter;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.market.integralgoods.fragment.IntegralGoodsFragment;
import com.ywjt.platformbusiness.util.JsonUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class IntegralMallActivity extends BaseActivity {
    private List<CommonModel> mTitleList;
    private List<Fragment> fragmentList;
    private RecyclerView rcType;
    private HomeGoodsTypeAdapter typeAdapter;
    private ViewPager viewPager;
    private MainAdapter adapter;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, IntegralMallActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.home_goods_type;
    }

    @Override
    protected void initView() {
        setTitleText("积分商城");
        rcType=findViewById(R.id.rcType);
        viewPager=findViewById(R.id.viewPager);
        mTitleList=new ArrayList<>();
        fragmentList=new ArrayList<>();


        getData();

    }

    @Override
    protected void initData() {

    }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     *
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }

    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position).getStr();
        }
    }

    //获取一级分类
    public void getData(){
        new HttpRequest(getContext()).doGet(UrlConstants.getFirstClassify, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                //把请求得数据转换成json对象
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), FistLevelTypeModel.class);
                        if (obj instanceof FistLevelTypeModel) {
                            FistLevelTypeModel levelTypeModel = (FistLevelTypeModel) obj;
                            for (int i = 0; i < levelTypeModel.getResult().size(); i++) {
                                if (i == 0) {
                                    mTitleList.add(new CommonModel(levelTypeModel.getResult().get(i).getName(), true));
                                }else {
                                    mTitleList.add(new CommonModel(levelTypeModel.getResult().get(i).getName(), false));
                                }
                                fragmentList.add(IntegralGoodsFragment.newInstance(levelTypeModel.getResult().get(i).getId()));
                            }

                            typeAdapter = new HomeGoodsTypeAdapter(mTitleList);
                            rcType.setLayoutManager(new LinearLayoutManager(IntegralMallActivity.this, RecyclerView.HORIZONTAL, false));
                            rcType.setAdapter(typeAdapter);
                            typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    for (int i = 0; i < mTitleList.size(); i++) {
                                        mTitleList.get(i).setSelected(false);
                                    }
                                    mTitleList.get(position).setSelected(true);
                                    typeAdapter.notifyDataSetChanged();
                                    viewPager.setCurrentItem(position);
                                }
                            });

                            adapter = new MainAdapter(getSupportFragmentManager());
                            viewPager.setAdapter(adapter);
                            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                                @Override
                                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                                    for (int i = 0; i < mTitleList.size(); i++) {
                                        mTitleList.get(i).setSelected(false);
                                    }
                                    mTitleList.get(position).setSelected(true);
                                    typeAdapter.notifyDataSetChanged();
                                }

                                @Override
                                public void onPageSelected(int position) {

                                }

                                @Override
                                public void onPageScrollStateChanged(int state) {

                                }
                            });
                        }
                        }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }
}
